package com.chen.androidstudy.activity

import android.Manifest
import android.content.pm.PackageManager
import android.media.MediaMetadataRetriever
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore
import android.util.Log
import android.widget.Toast
import com.base.library.base.BaseVMActivity
import com.chen.androidstudy.R
import com.chen.androidstudy.databinding.ActivityGetAudioFileTypeBinding
import com.chen.androidstudy.utils.AudioFormatExampleUtil

/**
 *
 * @Author:        CHong
 * @CreateDate:    2024/7/23 0023
 * @Description: 获取文件格式
 */
class GetAudioFileTypeActivity : BaseVMActivity<LocationViewModel, ActivityGetAudioFileTypeBinding>() {
    override fun getLayoutId(): Int {
        return R.layout.activity_get_audio_file_type
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
            && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
        ) {
            getTextFile()
        } else {
            requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE), 1)
        }
    }

    private fun getTextFile() {
//        var path = Environment.getExternalStorageDirectory().absolutePath + "/1/1.m4a" //mp4
//        var path = Environment.getExternalStorageDirectory().absolutePath + "/1/2.mp4" //mp4
//        var path = Environment.getExternalStorageDirectory().absolutePath + "/1/3.amr"  //amr-wb
//        var path = Environment.getExternalStorageDirectory().absolutePath + "/1/4.pcm"
//        var path = Environment.getExternalStorageDirectory().absolutePath + "/1/5.mp3" //mpeg
        var path = Environment.getExternalStorageDirectory().absolutePath + "/1/6.wav" //x-wav
        Log.e("chen", "type:" + AudioFormatExampleUtil.getAudioFormat(path))
        AudioFormatExampleUtil.getAudioFormat2(this)
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == 1) {
            if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // 权限被授予，发送短信
                Toast.makeText(this@GetAudioFileTypeActivity, "有定位的权限", Toast.LENGTH_SHORT).show()
                getTextFile()
            } else {
                // 权限被拒绝，可以提示用户或者采取其他行动
                Toast.makeText(this@GetAudioFileTypeActivity, "无定位权限", Toast.LENGTH_SHORT).show()
            }
        }
    }
}